package com.rew.canteenchefs.controllers;

import com.rew.canteenchefs.models.EDishes;
import com.rew.canteenchefs.models.EUser;
import com.rew.canteenchefs.models.Result;
import com.rew.canteenchefs.services.imp.EDishesService;
import com.rew.canteenchefs.services.imp.EUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static com.rew.canteenchefs.enumerate.ResultTypeEnum.SUCCESS;

/**
 * 学生端.
 *
 * Created by HuiWen Ren on 2016/12/5.
 */
@RestController
@RequestMapping("/canteenchefs/mobile")
public class ESTU {
    private static final Logger LOGGER = LoggerFactory.getLogger(ESTU.class);

    @Autowired
    EUserService eUserService;

    @Autowired
    EDishesService eDishesService;

    /**
     * 个人用户信息查询.
     *
     * @param card_id 校园卡号
     * @return 用户信息
     */
    @RequestMapping(value = "/select/user/{card_id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<EUser>>
    selectUser(@PathVariable("card_id") String card_id){
        LOGGER.info("receive request>>selectUser: [{}], card_id");
        return ResponseEntity.ok(eUserService.selectUser(card_id));
    }

    /**
     * 用户信息更新.
     *
     * @param eUser 更新信息
     * @return 更新结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result>
    updateUser(@RequestBody EUser eUser){
        LOGGER.info("receive request>>updateUser: [{}], eUser");
        return ResponseEntity.ok(eUserService.updateUser(eUser));
    }


    /**
     * 菜品信息查询.
     *
     * @param dishName 菜品名称
     * @return 用户信息
     */
    @RequestMapping(value = "/select/dish/{dishName}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result<EDishes>>
    selectDishes(@PathVariable("dishName") String dishName){
        LOGGER.info("receive request>>selectDishes: [{}], dishName");
        return ResponseEntity.ok(eDishesService.selectDishes(dishName));
    }

    /**
     * 连接测试.
     *
     * @param testCode wanna 1
     * @return rightCode success 1
     */
    @RequestMapping(value = "/test/connect/{testCode}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Integer>
    testConnect(@PathVariable("testCode") Integer testCode){
        LOGGER.info("receive request>>selectDishes: [{}], testCode");
        return ResponseEntity.ok(SUCCESS.getValue());
    }
}
